﻿using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using ELab.Client;
using ELab.Communication.Messages;
using ELab.Message;
using ELab.Serialization.Data;
using ELab.Communication.Messages.ControllerMessages;
using System.Collections.Generic;
using ELab.Settings;
using ELab.Communication;
using ELab.Management;
using System.Net;
using System.Net.Sockets;
using ELab.Core;

namespace ELab.Controls
{
    public partial class LoginControl : UserControl
    {
        public ELabController Controller { get; set; }
        public User CurrentUser { get; private set; }
        public event EventHandler OnCancelLogin;
        public event EventHandler OnLoginAnotherMachine;
        public event EventHandler OnLoginSuccess;
        public UserType UserType { get; set; }
        public LoginControl()
        {
            InitializeComponent();
            SetupPosition();
            UserType = UserType.Student;
        }

        private void LoginControl_Load(object sender, EventArgs e)
        {
            var form = this.FindForm();
            if (form != null)
            {
                form.AcceptButton = btnLogin;
                form.CancelButton = btnCancel;
            }
        }

        private void SetupPosition()
        {
            if (pnlContainer.InvokeRequired)
            {
                SetupPosition();
            }
            else
            {
                pnlContainer.Location = new Point(this.ClientSize.Width / 2 - pnlContainer.Size.Width / 2, this.ClientSize.Height / 2 - pnlContainer.Size.Height / 2);
                pnlContainer.Anchor = AnchorStyles.None;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (OnCancelLogin != null) OnCancelLogin(sender, e);
            this.FindForm().Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            int attent = 0, maxattent = SystemConfiguration.ReconnectTimes;
            while (attent < maxattent)
            {
                try
                {
                    var user = new CheckAuthenticationMessage { UserName = txtUserName.Text, Password = txtPassword.Text };
                    if (this.Controller != null)
                    {
                        Controller.ReConnectServerOnError = true;
                        Controller.Connect();
                        var ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); ;
                        var resPonseBase = this.Controller.SendMessageAndGetResponse(user);
                        if (resPonseBase != null)
                        {
                            var resPonse = resPonseBase as CheckAuthenticationResponseMessage;
                            if (resPonse.IsSuccess)
                            {
                                this.CurrentUser = resPonse.UserLogin;
                                if (this.CurrentUser.Type == UserType.Teacher || this.CurrentUser.Type == this.UserType)
                                {
                                    if (OnLoginSuccess != null) OnLoginSuccess(sender, e);
                                }
                            }
                        }
                    }
                    break;
                }
                catch (Exception es)
                {
                    attent++;
                    if (attent >= maxattent) throw es;
                }
                finally
                {
                    if (Controller != null) Controller.Disconnect();
                }
            }
        }
    }
}
